package com.ruoyi.web.controller.interview;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.interview.domain.vo.IntervInfoVo;
import com.ruoyi.interview.service.IIntervInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

import static com.ruoyi.common.core.page.MySetPage.mySetPage;


@RestController
@RequestMapping("/interview/info")
public class IntervInfoController extends BaseController
{
    @Autowired
    private IIntervInfoService intervInfoService;


    /**
     * 查询面试信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(IntervInfoVo intervInfoVo)
    {
        startPage();
        List<IntervInfoVo> intervInfoVos = intervInfoService.selectIntervInfoList(intervInfoVo);
        return mySetPage(intervInfoVos);
    }

    /**
     * 导出面试信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "导出功能", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IntervInfoVo intervInfoVo)
    {
        List<IntervInfoVo> list = intervInfoService.selectIntervInfoList(intervInfoVo);
        ExcelUtil<IntervInfoVo> util = new ExcelUtil<IntervInfoVo>(IntervInfoVo.class);
        util.exportExcel(response, list, "面试数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(intervInfoService.selectIntervInfoById(id));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody IntervInfoVo intervInfoVo)
    {
        return toAjax(intervInfoService.insertIntervInfo(intervInfoVo));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody IntervInfoVo intervInfoVo)
    {
        return toAjax(intervInfoService.updateIntervInfo(intervInfoVo));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)

    {
        return toAjax(intervInfoService.deleteIntervInfoByIds(ids));
    }

    /**
     * 导入
     */
    @PreAuthorize("@ss.hasPermi('system:info:import')")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        /** 创建Excel工具类 */
        ExcelUtil<IntervInfoVo> util = new ExcelUtil<IntervInfoVo>(IntervInfoVo.class);
        /** 获取导入数据 */
        List<IntervInfoVo> InfoVoList = util.importExcel(file.getInputStream());
        /** 获取当前登录用户,作为导入数据的创建人 */
        String message = intervInfoService.importUser(InfoVoList, updateSupport, "abc");
        return AjaxResult.success("导入ok~");

    }


    @PreAuthorize("@ss.hasPermi('system:info:import')")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<IntervInfoVo> util = new ExcelUtil<IntervInfoVo>(IntervInfoVo.class);
        return util.importTemplateExcel("用户数据");
    }




    /** 查询所有interviewee 参数为interviewee*/
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping("/selectInterviewee/{data}")
    public AjaxResult selectInterviewee(@PathVariable("data") String interviewee){

        /** 遍历intervInfoService.selectInterviewAll()的数据与interviewee 比较 */
        List<String> list = intervInfoService.selectInterviewwAll();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(interviewee)){
                return AjaxResult.success("姓名重复，生成乱码后缀");
            }
        }

        return AjaxResult.success("无重复面试者");
    }




}
